<?php
namespace Info\Controller;
use Common\Model\CommentModel;
use Common\Model\NewsCommentModel;
use Info\Controller\CommonController;

class InfoController extends CommonController {
    
    public function index(){
        $id = I('id');
        if(!empty($id)){
            $data = $this->getInfoById($id);
            if(!empty($data)){
                //获取父级栏目id
                $cateogry = $this->getFatherCategory($data['cid']);
                //相关栏目
                $HotCategory = $this->getInfoCategory($data['cid'],$cateogry['father']['id']);
                //相关信息
                if(!empty($data['phone'])){
                    $data['phoneTimes'] = $this->getInfosByPhone($data['phone']);
                }else{
                    $data['phoneTimes'] = 0;
                }

                $commentModel = new CommentModel();
                $comment = $commentModel->getComByInfoId($id);

                $idArr = array_column($HotCategory,'id');
                $hotInfo = $this->getInfoInId($idArr);
                $area = C("city_area");
                $this->assign('data',$data)->assign('category',$HotCategory)
                    ->assign("hotInfo",$hotInfo)
                    ->assign("fatherCateogry",$cateogry['father'])
                    ->assign('currentCategory',$cateogry['current'])
                    ->assign('comment',$comment)
                    ->assign("area",$area);
                $this->display(':info');
                exit();
            }else{
                //信息已过期或删除
                $this->redirect('/info/info/error404',array('id'=>$id));
            }
        }
        $this->redirect("/info/index");
    }


    public function comment(){
        if(IS_POST){
            $isInfo = I("get.isInfo");
            if($isInfo){
                $model = new CommentModel();
            }else{
                $model = new NewsCommentModel();
            }
            if($model->addComment()){
                $this->success('评论发布成功,请等待审核');
            }else{
                $this->error($model->getError());
            }
        }
    }

    public function error404(){
        $this->display(":404");
    }


}